package edu.zzuli.assistant.action;

import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import edu.zzuli.assistant.bean.Admin;
import edu.zzuli.assistant.service.AdminService;
import edu.zzuli.assistant.service.impl.AdminServiceImpl;
import edu.zzuli.assistant.until.StrUtil;
public class AdminAction extends ActionSupport implements SessionAware {

	private AdminService adminService = new AdminServiceImpl();

	private Map<String, Object> session;

	private Admin admin = null;

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Object getModel() {
		// TODO Auto-generated method stub
		if (admin == null) {
			this.admin = new Admin();
		}
		return admin;
	}

	// 重定向到首页
	private void redirectIndex() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String contextPath = ((HttpServletRequest) request).getContextPath();
		try {
			response.sendRedirect(contextPath + "/index.action");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 管理员登陆验证
	 * @return
	 */
	public void login() {
		this.getModel();
		String status = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String refer = request.getHeader("referer");
		// 用refer 拦截
		if (refer == null || refer.indexOf("/assistant/") < 0) {
			redirectIndex();
			return;
		}
		// 登陆验证
		if (admin.getId() == null || admin.getPassword() == null) {
			StrUtil.sendMsg("fail");
		} else {
			// 判断用户是否存在
			Admin result = adminService.login(admin);
			if (result!=null) {
				if (session.get("userId")!=null) {
					session.clear();
				}
				session.put("id", result.getId());
				session.put("name", result.getName());
				status = "success";
				StrUtil.sendMsg("success");
			} else {
				StrUtil.sendMsg("fail");
			}
		}
	}
    //开通公共号admin_addPublic.action,跳到开通公共号界面
	public String addPublic(){
		if(session.get("id")!=null){
			return "admin_user";
		}
		return "admin_login";
	}
	//管理员发通知admin_addNotice.action,跳到发帖的界面
	public String addnotice(){
		if(session.get("id")!=null){
			return "admin_post";
		}
		return "admin_login";
	}
	//管理员进入管理入口(admin_come.action)
	public String come(){
			if (session.get("id")!=null) {
				
				return "admin_adNotice";
			}
			else{
				
				return "admin_login";
			}
	}
//	管理员修改自己的密码admin_modifyPassword.action
	public void modifyPassword() {
		if (session.get("id") != null) {
	       admin.setId(session.get("id").toString());
			HttpServletRequest request = ServletActionContext.getRequest();
			String newPassword = request.getParameter("newPassword");
			if ( newPassword.length() < 6|| newPassword.length() > 12) {
				StrUtil.sendMsg("fail");
				return;
			}
			Admin a =adminService.login(admin);
			if (a!=null) {
				boolean result=adminService.modifyPassword(admin, newPassword);
				if (result) {
					StrUtil.sendMsg("success");
				} else {
					StrUtil.sendMsg("fail");
				}
			} else {
				StrUtil.sendMsg("password_error");
			}
		}
		else{
			redirectIndex();
		}
	}
	//跳到管理员修改自己密码的页面
	public String account(){
		if (session.get("id")!=null) {
			return "admin_account";
		}
		return "fail";
	}
}
